package control
{
	import control.panel.PanelControl;
	
	import events.ItemEvent;
	import events.PlayEvent;
	
	import global.GameState;
	
	import model.load.XmlLoadMessage;
	import model.room.ItemData;
	
	import utils.OpenWindow;
	
	import view.components.list.ITileListItem;
	import view.components.list.TileList;
	import view.panel.item.ItemPanel;
	
	public class ItemPanelControl extends PanelControl
	{
		public static const ITEM_SHOP:int=0;
		public static const ITEM_OWN:int=1;

		private var shopLoaded:Boolean;
		private var currentType:int;
		private var lastXmlLoadMsg:XmlLoadMessage;
		private var currentIndex:int;
		
		public var currentSitId:String;
		
		public function ItemPanelControl()
		{
			initPanel();
			initListener();

		}
		
		private function initListener():void
		{
			_model.playerModel.addEventListener(PlayEvent.SHOW_SHOP_ITEM_LIST,onShopItemList);
			_model.itemModel.addEventListener(ItemEvent.ITEM_ARRIVAL,onItemList);
			_model.itemModel.addEventListener(ItemEvent.ITEM_USE_RESULT,itemUseResult);
				
		}
		
		private function itemUseResult(event:ItemEvent):void
		{
			var item:ITileListItem
			for (var i:int=0;i<_view.itemPanel.ownList.count;i++)
			{
				item = _view.itemPanel.ownList.getItemByIndex(i);
				if (item.data.@itemId == event.itemId)
				{
					item.data.@count = event.count;
					item.update(item.data);
					return;
				}
			}
			
		}
		
		private function onItemList(event:ItemEvent):void
		{
			
			var xml:XML = <itemList></itemList>;
			
			for each (var data:ItemData in event.itemDatas)
			{
				var itemXml:XML = <item></item>;
				itemXml.@itemId = data.id;
				itemXml.@name = data.name;
				itemXml.@image = data.image;
				itemXml.@desc = data.desc;
				itemXml.@count = data.count;
				itemXml.@canUse = 1;
				xml.appendChild(itemXml);
			}
			
		
			onXmlBack(xml,ITEM_OWN);
			
			//
		}
		
		private function onShopItemList(event:PlayEvent):void
		{
			if (shopLoaded)
			{
				return;
			}
			
			shopLoaded = true;
			var xml:XML = <itemList></itemList>;
			
			for each (var data:ItemData in event.cardDatas)
			{
				var itemXml:XML = <item></item>;
				itemXml.@itemId = data.id;
				itemXml.@name = data.name;
				itemXml.@image = data.image;
				itemXml.@desc = data.desc;
				itemXml.@count = data.count;
				itemXml.@price = data.price;
				itemXml.@canUse = data.canUse;
				xml.appendChild(itemXml);
			}
			
			
			onXmlBack(xml,ITEM_SHOP);
	
		}
		
		
		private function onXmlBack(data:XML,type:int):void
		{
			var list:TileList;
			if (type == ITEM_SHOP)
			{
				list = _view.itemPanel.shopList;
			}
			else if (type == ITEM_OWN)
			{
				list = _view.itemPanel.ownList;
				lastXmlLoadMsg = null;
			}
			list.removeAll();
			for each (var xml:XML in data.children())
			{
				
				list.addItem(xml);
				
			}
			
			if (currentType == type)
			{
				_view.itemPanel.hideLoading();
			}
			
			trace(data);
			
			
		}
		
		private function initPanel():void
		{
			_view.itemPanel = new ItemPanel(this);
		}
		
		public function onTabSwitch(index:int):void
		{
			currentIndex = index;
			if (GameState.isInGameRoom)
			{
				return;
			}
			
			if (lastXmlLoadMsg)
			{
				lastXmlLoadMsg.cancel();
			}
			
			_view.itemPanel.hideLoading();
			_view.itemPanel.ownList.removeAll();
			if (index == 1)
			{
				_view.itemPanel.showLoading();
				var loadMessage:XmlLoadMessage = new XmlLoadMessage(onXmlBack);
				loadMessage.id = ITEM_OWN;
				loadMessage.url = GameState.getServerLink() + "30&key=" + GameState.snsKey + "&type=" + ITEM_OWN;
				loadMessage.send();
				
				currentType = ITEM_OWN;
				lastXmlLoadMsg = loadMessage;
			}
		
		}
		
		
		
		public function open(switchToMyITem:Boolean = false,switchToPresent:Boolean = false):void
		{
			_view.itemPanel.open();
			
			loadShopList();
			
			if (switchToMyITem)
			{
				_view.itemPanel.switchToMyItem();
			}
			
			
			
			if (switchToPresent)
			{
				_view.itemPanel.ownList.switchItemsState(1);
			}
			else
			{
				_view.itemPanel.ownList.switchItemsState(0);
			}
		}
		
		public function loadShopList():void
		{
			if ( !shopLoaded  && !GameState.isInGameRoom)
			{
				_view.itemPanel.showLoading();
				var loadMessage:XmlLoadMessage = new XmlLoadMessage(onXmlBack);
				loadMessage.id = ITEM_SHOP;
				loadMessage.url = GameState.getServerLink() + "30&key=" + GameState.snsKey + "&type=" + ITEM_SHOP;
				loadMessage.send();
				
				currentType = ITEM_SHOP;
				shopLoaded = true;
			}
		}
		
		public function useItem(itemId:String):void
		{
			
			if (GameState.isInGameRoom)
			{
				_model.netRemote.useItem(itemId);
				return;
			}
			
			var loadMessage:XmlLoadMessage = new XmlLoadMessage(onItemUseBack);
			loadMessage.url = GameState.getServerLink() + "32&key=" + GameState.snsKey + "&itemId=" + itemId;
			loadMessage.send();
		}
		
		private function onItemUseBack(data:XML,type:int):void
		{
			var result:int = int(data.@result);
			if (result == 1)
			{
				_control.alertButPanelControl.alert("使用成功！","");
				_control.playerInfoModulControl.refreshInfo();
			}
			else
			{
				_control.alertButPanelControl.alert("使用失败！","");
			}
			
			var lastCount:int = int(data.@count);
			var itemId:String = String(data.@itemId);
			var item:ITileListItem = _view.itemPanel.ownList.seachItem("itemId",itemId);
			
			if (item == null)
			{
				return;
			}
			
			if (lastCount == 0)
			{	
				_view.itemPanel.ownList.removeItemAt(item.index);	
			}
			else
			{
				item.update(data);
			}
		}
		
		public function onGotoShop():void
		{
			if (GameState.isInGameRoom)
			{
				//navigateToURL(new URLRequest("http://apps.renren.com/openyou/pay.php"),"_blank");
				
				OpenWindow.openWindow(GameState.getShopLink());
			}
			else
			{
				OpenWindow.openWindow(GameState.getShopLink(),"_top");
			}
			
		}
		
		//赠送道具
		public function sendItem(itemId:String,count:String):void
		{
			_view.itemPanel.close();
			_model.netRemote.sendItem(currentSitId,itemId,count);
		}
		
		public function clear():void
		{
			_view.itemPanel.switchToShopItem();
		}
	}
}